# coding=utf-8

from application import app, db
from common.libs.JsonLibs import bytes_to_obj
from app.model.mysql.safebox import Safebox as SafeboxMysql
from app.model.mysql.user_gift import UserGift as UserGiftMysql
from app.json.mysql.safebox import Safebox as SafeboxJson

from common.libs.DateTimeLibs import get_now_time_str, get_now_time, date_format, date_time_cal_str, ONLY_TIME, ONLY_DATE


def get_list(token):
    if not token:
        return False
    user_id = token.split("#")[1] if "#" in token else 0
    if user_id == 0:
        return False
    mysql_list = SafeboxMysql.query.filter(SafeboxMysql.uid == user_id, SafeboxMysql.is_delete == 0).order_by(SafeboxMysql.g_id.asc()).all()
    json_list = []
    for mysql_item in mysql_list:
        json_item = SafeboxJson(mysql_item).getJson()
        json_list.append(json_item)
    return json_list


def save(token, num):
    if not token:
        return False
    user_id = token.split("#")[1] if "#" in token else 0
    if user_id == 0:
        return False
    try:
        mysql_safebox_one = SafeboxMysql.query.filter(SafeboxMysql.uid == user_id, SafeboxMysql.is_delete == 0,
                                                      SafeboxMysql.g_id == 1).order_by(SafeboxMysql.g_id.asc()).first()
        mysql_usergift_one = UserGiftMysql.query.filter(UserGiftMysql.u_id == user_id, UserGiftMysql.is_delete == 0,
                                                        UserGiftMysql.g_id == 1).order_by(UserGiftMysql.g_id.asc()).first()

        if not mysql_safebox_one:
            return False
        if not mysql_usergift_one:
            return False

        if mysql_usergift_one.num < int(num):
            return False
        temp_time = get_now_time()

        mysql_safebox_info = mysql_safebox_one
        mysql_safebox_info.num = mysql_safebox_one.num + int(num)
        mysql_safebox_info.updated_time = date_format(temp_time)

        mysql_usergift_info = mysql_usergift_one
        mysql_usergift_info.num = mysql_usergift_one.num - int(num)
        mysql_usergift_info.updated_time = date_format(temp_time)

        if mysql_safebox_info.num + mysql_usergift_info.num != mysql_usergift_info.all_num:
            return False

        db.session.add(mysql_safebox_info)
        db.session.add(mysql_usergift_info)
        db.session.flush()
        db.session.commit()
        return True
    except Exception as e:
        print(e)
        db.session.rollback()
        return False


def take(token, num):
    if not token:
        return False
    user_id = token.split("#")[1] if "#" in token else 0
    if user_id == 0:
        return False
    try:
        mysql_safebox_one = SafeboxMysql.query.filter(SafeboxMysql.uid == user_id, SafeboxMysql.is_delete == 0, SafeboxMysql.g_id == 1).order_by(SafeboxMysql.g_id.asc()).first()
        mysql_usergift_one = UserGiftMysql.query.filter(UserGiftMysql.u_id == user_id, UserGiftMysql.is_delete == 0, UserGiftMysql.g_id == 1).order_by(UserGiftMysql.g_id.asc()).first()

        if not mysql_safebox_one:
            return False
        if not mysql_usergift_one:
            return False

        if mysql_safebox_one.num < int(num):
            return False
        temp_time = get_now_time()

        mysql_safebox_info = mysql_safebox_one
        mysql_safebox_info.num = mysql_safebox_one.num - int(num)
        mysql_safebox_info.updated_time = date_format(temp_time)

        mysql_usergift_info = mysql_usergift_one
        mysql_usergift_info.num = mysql_usergift_one.num + int(num)
        mysql_usergift_info.updated_time = date_format(temp_time)

        if mysql_safebox_info.num + mysql_usergift_info.num != mysql_usergift_info.all_num:
            return False

        db.session.add(mysql_safebox_info)
        db.session.add(mysql_usergift_info)
        db.session.flush()
        db.session.commit()
        return True
    except Exception as e:
        print(e)
        db.session.rollback()
        return False